package com.qing.common.base;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.bumptech.glide.Glide;

/**
 * @author qing <br/>
 * 2025/3/2
 */
public abstract class BaseButton extends FrameLayout {

    protected static final int INFO_CONTENT = 1;
    protected static final int INFO_BACKGROUND = 2;
    private static final String TAG = "BaseButton";
    protected final SparseArray<SparseIntArray> mTypeInfos = new SparseArray<>();
    private LottieAnimationView mImageView;
    private int mType;

    public BaseButton(@NonNull Context context) {
        this(context, null);
    }

    public BaseButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public BaseButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        mImageView = new LottieAnimationView(getContext());
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        mImageView.setLayoutParams(params);
        addView(mImageView);
        onInit();
    }

    protected abstract void onInit();

    public final int getType() {
        return mType;
    }

    public final void setType(int type) {
        int oldType = mType;
        mType = type;
        post(() -> {
            onTypeChanged(oldType, mType);
        });
    }

    @NonNull
    protected LottieAnimationView getImageView() {
        return mImageView;
    }

    /**
     * 默认资源改变时，更新按钮样式，当资源为raw时，播放动画，否则加载图片，可以重写该方法
     */
    protected void onTypeChanged(int lastType, int newType) {
        if (mTypeInfos != null) {
            SparseIntArray info = mTypeInfos.get(getType());
            if (info != null) {
                int contentRes = info.get(INFO_CONTENT);
                if (contentRes != 0) {
                    if (getResources().getResourceTypeName(contentRes).equals("raw")) {
                        mImageView.setAnimation(contentRes);
                        mImageView.setRepeatCount(ValueAnimator.INFINITE);
                        mImageView.playAnimation();
                    } else {
                        Glide.with(mImageView)
                                .load(contentRes)
                                .transition(withCrossFade())
                                .into(mImageView);
                    }
                }
                int backgroundRes = info.get(INFO_BACKGROUND);
                if (backgroundRes != 0) {
                    setBackgroundResource(backgroundRes);
                }
            }
        }
    }

}
